package com.easybuilder.core.table.controller;

import com.easybuilder.common.base.controller.BaseController;
import com.easybuilder.common.utils.StringUtil;
import com.easybuilder.core.table.entity.SysTableInfo;
import com.easybuilder.core.table.service.TableInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/tableinfo")
public class TableInfoController extends BaseController {
    @Autowired
    private TableInfoService tableInfoService;

    /** 保存 */
    @PostMapping("/save")
    public Result<Object> save(@RequestBody List<SysTableInfo> list) {
        if (list == null || list.size() == 0) {
            return Error("数据不能为空");
        }
        for (SysTableInfo sysTableInfo : list) {
            if (StringUtil.isEmpty(sysTableInfo.getColumnName())) {
                return Error("columnName不能为空");
            }
            if (StringUtil.isEmpty(sysTableInfo.getColumnType())) {
                return Error("columnType不能为空");
            }
            if (null == sysTableInfo.getTableId()) {
                return Error("tableId不能为空");
            }
        }
        list = tableInfoService.save(list);
        return OK(list);
    }

    /** 查询 */
    @GetMapping("/select/{table_id}")
    public Result<Object> select(@PathVariable(name = "table_id", required = true) Long table_id) {
        List<SysTableInfo> list = tableInfoService.selectByTableId(table_id);
        return OK(list);
    }
}
